package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.ApiResult;
import com.example.entity.Grade;
import com.example.service.GradeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "年级管理", description = "年级的增删改查")
@RestController
@RequestMapping("/grade")
public class GradeController {
    @Autowired
    private GradeService gradeService;

    @Operation(summary = "新增年级", description = "新增一条年级记录")
    @ApiResponses({
        @ApiResponse(responseCode = "0", description = "新增成功"),
        @ApiResponse(responseCode = "-1", description = "新增失败")
    })
    @PostMapping
    public ApiResult<Grade> addGrade(
            @RequestBody @Parameter(description = "年级实体", required = true) Grade grade) {
        boolean success = gradeService.save(grade);
        if (success) {
            return ApiResult.success("新增成功", grade);
        } else {
            return ApiResult.error("新增失败");
        }
    }

    @Operation(summary = "修改年级", description = "根据ID修改年级信息")
    @ApiResponses({
        @ApiResponse(responseCode = "0", description = "修改成功"),
        @ApiResponse(responseCode = "-1", description = "修改失败")
    })
    @PutMapping
    public ApiResult<Boolean> updateGrade(
            @RequestBody @Parameter(description = "年级实体，需包含ID", required = true) Grade grade) {
        boolean success = gradeService.updateById(grade);
        return success ? ApiResult.success("修改成功", true) : ApiResult.error("修改失败");
    }

    @Operation(summary = "删除年级", description = "根据ID删除年级")
    @ApiResponses({
        @ApiResponse(responseCode = "0", description = "删除成功"),
        @ApiResponse(responseCode = "-1", description = "删除失败")
    })
    @DeleteMapping("/{id}")
    public ApiResult<Boolean> deleteGrade(
            @PathVariable @Parameter(description = "年级ID", required = true) Long id) {
        boolean success = gradeService.removeById(id);
        return success ? ApiResult.success("删除成功", true) : ApiResult.error("删除失败");
    }

    @Operation(summary = "查询单个年级", description = "根据ID查询年级信息")
    @ApiResponses({
        @ApiResponse(responseCode = "0", description = "查询成功"),
        @ApiResponse(responseCode = "-1", description = "未找到年级")
    })
    @GetMapping("/{id}")
    public ApiResult<Grade> getGrade(
            @PathVariable @Parameter(description = "年级ID", required = true) Long id) {
        Grade grade = gradeService.getById(id);
        if (grade == null) {
            return ApiResult.notFound("未找到年级");
        }
        return ApiResult.success("查询成功", grade);
    }

    @Operation(summary = "年级列表（分页）", description = "分页获取年级列表，pageNum=0且pageSize=0时返回全部")
    @ApiResponses({
        @ApiResponse(responseCode = "0", description = "查询成功")
    })
    @GetMapping("/list")
    // ApiResult<?>用法解释：
    // 1. <?>表示泛型的通配符，表示可以接受任何类型的数据。
    // 2. 在这里使用ApiResult<?>，是因为根据请求参数的不同，返回的数据类型可能不同。
    //    - 当pageNum和pageSize都为0时，返回的是List<Grade>类型的全部年级列表。
    //    - 否则，返回的是IPage<Grade>类型的分页结果。
    public ApiResult<?> listGrades(
            @RequestParam(defaultValue = "1") @Parameter(description = "页码，0为不分页") int pageNum,
            @RequestParam(defaultValue = "10") @Parameter(description = "每页数量，0为不分页") int pageSize) {
        if (pageNum == 0 && pageSize == 0) {
            // 不分页，返回全部
            return ApiResult.success("查询成功", gradeService.list());
        } else {
            Page<Grade> page = new Page<>(pageNum, pageSize);
            IPage<Grade> result = gradeService.page(page);
            return ApiResult.success("查询成功", result);
        }
    }

}
